package com.yyf.springboot2.controller;

import com.alibaba.fastjson.JSONObject;
import com.yyf.springboot2.common.DateAndTimeUtils;
import com.yyf.springboot2.common.GenerateUtil;
import com.yyf.springboot2.common.RegularUtils;
import com.yyf.springboot2.common.ResultBean;
import com.yyf.springboot2.entity.TUser;
import com.yyf.springboot2.service.TUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyf
 * @since 2019-05-29
 */
@Controller
@RequestMapping("/tUser")
public class TUserController {

    @Resource
    private TUserService userService;



    /**
     * 随机用户名
     *
     * @return
     */
    String getUsernameByRand() {
        String name = "";
        //获取系统设置的用户名前缀
        String userName = name + new Random().nextInt(99999999) + "";
        while (userName.length() != name.length() + 8
                || userService.getUserInfoByUsername(userName) != null) {
            userName = name + new Random().nextInt(99999999) + "";
        }
        return userName;
    }

    //获取用户信息用于展示
    @GetMapping("/userInfo")
    private ResultBean userInfo(Integer id){
        if( id != null && id.intValue() >= 0){
            //根据id获取用户信息


        }
        return ResultBean.ok();

    }

    @ResponseBody
    @PostMapping("/register")
    private  ResultBean registerUser(){

        return null;
    }



    @GetMapping("/amount")
    @ResponseBody
    public  ResultBean amountUser(@RequestParam(value = "token") String token){
        //获取用户资金信息
        Integer uid= 1;
        TUser user = userService.getUserAmount(uid);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("user",user);
        JSONObject jsonObject = new JSONObject(map);

        return ResultBean.build(200,"成功",jsonObject);

    }

    @GetMapping("/detail")
    @ResponseBody
    public  ResultBean userDetail(@RequestParam(value = "token") String token){
        Integer userId = 1;
 //       try {
//         String   uId = AuthToken.decrypt(token,1);
//         userId = Integer.valueOf(uId);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if(userId == null){
//            return  ResultBean.build(401,"token验证失败，请重新获取token",null);
//        }
        TUser user = userService.getUserAmount(userId);
        if (user == null){
            return  ResultBean.build(104,"用户不存在",null);
        }
        //获取省市区的初始信息

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("user",user);
        JSONObject jsonObject = new JSONObject(map);
        return  ResultBean.build(200,"成功",jsonObject);

        }



        @PostMapping("/tgRegUser")
        @ResponseBody
        public  ResultBean thRegUser(@RequestParam(value = "token") String token,
                                     @RequestParam(value = "name")String name,
                                     @RequestParam(value = "mobile")String mobile){

            Integer tjUserId = 1;
            //       try {
//         String   uId = AuthToken.decrypt(token,1);
//         tjUserId = Integer.valueOf(uId);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        if(tjUserId == null){
//            return  ResultBean.build(401,"token验证失败，请重新获取token",null);
//        }
            if(name==null || name.equals("")){
                return  ResultBean.build(401,"用户名不能为空",null);
            }
            if(!RegularUtils.isMobileNO(mobile)){
                return  ResultBean.build(401,"手机号格式不正确",null);
            }
            TUser tjUser = userService.getUserAmount(tjUserId);
            if(tjUser == null){
                return  ResultBean.build(104,"推荐用户不存在,请检查后重新注册",null);
            }

            //生成盐值
            String salt = GenerateUtil.getRandomSalt();
            // 给密码加盐
            String  password = GenerateUtil.md5AndSha("111111",salt);
            System.out.println(DateAndTimeUtils.getCurrentDateString());
            //设置新的账户,存推荐关系
            TUser tUser = new TUser();
            //生成用户名，
            tUser.setUsername(getUsernameByRand());
            tUser.setPassword(password);
            tUser.setTwoPassword(password);
            tUser.setThreePassword(password);
            tUser.setName(name);
            tUser.setPhone(mobile);
            tUser.setNetId(tjUser.getId());
            userService.save(tUser);
            Map<String,Object> map = new HashMap <>();
            tUser.setPassword("111111");
            map.put("regUser",tUser);
            JSONObject jsonObject = new JSONObject(map);
            return  ResultBean.build(200,"成功",jsonObject);

        }

}
